package com.yunbao.hujin.mymvpdemo3.mvp;

import com.yunbao.hujin.mymvpdemo3.request.ApiService;
import com.yunbao.hujin.mymvpdemo3.request.WeatherBean;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;

/**
 * M层 数据层 on 2017/12/7.
 */

public class RequestMode3 {

    private static final String BASE_URL = "http://www.weather.com.cn/";

    private Call<WeatherBean> call;

    /**
     * 请求数据Model层
     * 网络请求
     */
    //http://www.weather.com.cn/data/cityinfo/101010100.html---101210101(杭州)
    public void request(String detailId, Callback<WeatherBean> callback) {
        //请求接口
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(ScalarsConverterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        ApiService apiService = retrofit.create(ApiService.class);

        //请求
        call = apiService.requestWeather(detailId);
        call.enqueue(callback);
    }

    /**
     *  取消网络请求
     */
    public void interruptHttp(){
        if (call != null && !call.isCanceled()) {
            call.cancel();
        }
    }

}
